# -*- coding: utf-8 -*-
from django import forms

from web import models
from web.forms.bootstrap import BootStrapForm


class IssuesModelForm(BootStrapForm, forms.ModelForm):
    class Meta:
        model = models.Issues
        exclude = ['project', 'creator', 'create_datetime', 'latest_update_dateitme']
        
        # 重写组件 bootstrap-select
        widgets = {
            'assign': forms.Select(attrs={'class': 'selectpicker', 'data-live-search': 'true'}),
            'parent': forms.Select(attrs={'class': 'selectpicker', 'data-live-search': 'true'}),
            'attention': forms.SelectMultiple(attrs={
                'class': 'selectpicker',
                'data-live-search': 'true',
                'data-actions-box': "true"
            }),
        }
    
    def __init__(self, request, *args, **kwargs):
        super().__init__(*args, **kwargs)
        
        # 处理数据
        # 1. 获取当前项目的所有问题类型
        self.fields['issues_type'].choices = models.IssuesType.objects.filter(project=request.tracer.project).values_list('id', 'title')
        
        # 2. 获取当前下所以的模块
        module_list = [('', '没有选择项'), ]
        module_obj_list = models.IssuesType.objects.filter(project=request.tracer.project).values_list('id', 'title')
        module_list.extend(module_obj_list)
        self.fields['module'].choices = module_list
        
        # 3. 指派和关注者:只能是项目的参与者、创建者
        total_user_list = [(request.tracer.project.creator.id,  # 这里要给id,不然创建不了
                            request.tracer.project.creator.name), ]
        # 所有参与者的列表
        project_user_list = models.Participant.objects.filter(project=request.tracer.project).values_list('user_id', 'user__name')
        total_user_list.extend(project_user_list)
        # 因为assign是可以为空的 所以给它再加一项
        self.fields['assign'].choices = [('', '没有选择项'), ] + total_user_list
        self.fields['attention'].choices = total_user_list
        
        # 4. 当前创建的所有问题
        parent_list = [('', '没有选择项'), ]
        parent_obj_list = models.Issues.objects.filter(project=request.tracer.project).values_list('id', 'subject')
        parent_list.extend(parent_obj_list)
        self.fields['parent'].choices = parent_list


class IssuesReplyModelForm(forms.ModelForm):
    """回复讨论"""

    class Meta:
        model = models.IssuesReply
        fields = ['content', 'reply']


class InviteModelForm(BootStrapForm, forms.ModelForm):
    """邀请"""
    class Meta:
        model = models.ProjectInvite
        fields = ['period', 'count']